/*
 * @file     g_exit.c
 * @brief    source file for setting exit
 *
 * Change Logs:
 * Date            Author             Version        Notes
 * 2021-12-24      yangzhengfeng      V1.0.0         Modify the register module configuration
 * 2021-01-03      wangjingfan        V1.0.1         Compile error correction
 * 2021-01-20      yangzhengfeng      V1.0.2         Update library function
 */

#include "g_exti.h"

/**
  * @brief  Clear interrupt flag
  * @param  EXTI_Line:EXTI_Line_0...EXTI_Line_4
  * @param  EXTI_PinSource:EXTI_PinSource0...EXTI_PinSource15
  * @retval none
  */
void G_EXTI_ClearITPendingBit(EXTI_LineTypeDef EXTI_Line, EXTI_PIN_TypeDef EXTI_PinSource)
{
    _ASSERT(IS_EXTI_LINE(EXTI_Line));
    _ASSERT(IS_EXTI_PIN_SOURCE(EXTI_PinSource));

    MGPIO->IRQ_STATUS.reg[EXTI_Line] = EXTI_PinSource;
}

/**
  * @brief  Deinitializes the EXTI registers to default reset values.
  * @param  none
  * @retval none
  */
void G_EXTI_DeInit()
{
    uint32_t i;

    for (i = 0; i < EXIT_Num; i++)
    {
        MGPIO->INTR.reg[i] = 0;
        MGPIO->IRQ_LEVEL.reg[i] = 0;
        MGPIO->IRQ_RISE.reg[i] = 0;
        MGPIO->IRQ_FALL.reg[i] = 0;
    }
}

/**
  * @brief  get interrupt status
  * @param  EXTI_Line:EXTI_Line_0...EXTI_Line_4
  * @retval none
  */
uint16_t G_EXTI_GetITLineStatus(EXTI_LineTypeDef EXTI_Line)
{
    _ASSERT(IS_EXTI_LINE(EXTI_Line));

    return MGPIO->IRQ_STATUS.reg[EXTI_Line];
}

/**
  * @brief  get interrupt pinsource status
  * @param  EXTI_Line:EXTI_Line_0...EXTI_Line_4
  * @param  EXTI_PinSource:EXTI_PinSource0...EXTI_PinSource15
  * @retval none
  */
uint16_t G_EXTI_GetITEXTI_PinSourceStatus(EXTI_LineTypeDef EXTI_Line, EXTI_PIN_TypeDef EXTI_PinSource)
{
    _ASSERT(IS_EXTI_LINE(EXTI_Line));
    _ASSERT(IS_EXTI_PIN_SOURCE(EXTI_PinSource));

    if(MGPIO->IRQ_STATUS.reg[EXTI_Line] & EXTI_PinSource)
    {
        return ENABLE;
    }
    else
    {
        return DISABLE;
    }

}

/**
  * @brief  EXTI LineConfig
  * @param  EXTI_Line:EXTI_Line_0...EXTI_Line_4
  * @param  EXTI_PinSource:EXTI_PinSource0...EXTI_PinSource15
  * @param  EXTI_Trigger:EXTI Trigger mode
  * @retval none
  */
void G_EXTI_LineConfig(EXTI_LineTypeDef EXTI_Line, EXTI_PIN_TypeDef EXTI_PinSource, EXTI_TriggerTypeDef EXTI_Trigger)
{
    _ASSERT(IS_EXTI_LINE(EXTI_Line));
    _ASSERT(IS_EXTI_PIN_SOURCE(EXTI_PinSource));
    _ASSERT(IS_TRIG_TYPEDEF(EXTI_Trigger));

    if (EXTI_Trigger == EXTI_Trigger_Off)
    {
        MGPIO->INTR.reg[EXTI_Line] &= (~EXTI_PinSource);
        MGPIO->IRQ_LEVEL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_RISE.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_FALL.reg[EXTI_Line]  &= (~EXTI_PinSource);
    }
    else if (EXTI_Trigger == EXTI_Trigger_HighLev )
    {
        MGPIO->IRQ_LEVEL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_RISE.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_FALL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->INTR.reg[EXTI_Line] |= EXTI_PinSource;
    }
    else if (EXTI_Trigger == EXTI_Trigger_LowLev )
    {
        MGPIO->IRQ_LEVEL.reg[EXTI_Line]  |= EXTI_PinSource;
        MGPIO->IRQ_RISE.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_FALL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->INTR.reg[EXTI_Line] |= EXTI_PinSource;
    }
    else if (EXTI_Trigger == EXTI_Trigger_Rising )
    {
        MGPIO->IRQ_LEVEL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_RISE.reg[EXTI_Line]  |= EXTI_PinSource;
        MGPIO->IRQ_FALL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->INTR.reg[EXTI_Line] |= EXTI_PinSource;
    }
    else if (EXTI_Trigger == EXTI_Trigger_Falling )
    {
        MGPIO->IRQ_LEVEL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_RISE.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_FALL.reg[EXTI_Line]  |= EXTI_PinSource;
        MGPIO->INTR.reg[EXTI_Line] |= EXTI_PinSource;
    }
    else if (EXTI_Trigger == EXTI_Trigger_Rising_Falling )
    {
        MGPIO->IRQ_LEVEL.reg[EXTI_Line]  &= (~EXTI_PinSource);
        MGPIO->IRQ_RISE.reg[EXTI_Line]  |= EXTI_PinSource;
        MGPIO->IRQ_FALL.reg[EXTI_Line]  |= EXTI_PinSource;
        MGPIO->INTR.reg[EXTI_Line] |= EXTI_PinSource;
    }
}

#if 0
extern void VBAT_IRQHandler(void);
extern void EXTI_IRQHandler(void);

void GPIO_IRQHandler()
{
    if (MSYSCTRL->CHGR_EVENT_IRQ.bit.VBAT_OV & MSYSCTRL->CHGR_EVENT_ICTRL.bit.VBAT_OV_IE)
    {
        VBAT_IRQHandler();
    }
    else
    {
        EXTI_IRQHandler();
    }
}
#endif

/************************ (C) COPYRIGHT GKT *****END OF FILE****/
